<?php

class Model_Girl {

    static $TABLE_NAME = 'girl';
    static $TABLE_NAME_DETAIL = 'girl_detail';
    static $TABLE_NAME_TOP20 = 'girl_top20';
    static $TABLE_NAME_DETAIL_MODEL = 'girl_model_detail';
    static $TABLE_NAME_GIRL_ALBUM = 'girl_album';
    static $TABLE_NAME_GIRL_ALBUM_DETAIL = 'girl_album_detail';

    //girl album  
    public function search_album_by_title($title, $page, $count) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM;
        $start_from = ($page - 1) * $count;
        $end = $count;
        $sql .= " where title like '%" . $title . "%'";
        $sql .= ' order by id DESC ';
        $sql .= ' LIMIT ' . $start_from . ',' . $end;
        $data = $db->fetchAll($sql);
        if(!empty($data)){
            foreach ($data as $k => $value) {
                unset($data[$k]['img']);
                unset($data[$k]['comment']);
                unset($data[$k]['admin']);
                unset($data[$k]['create_time']);
            }
        }
        return $data;
    }
    
    public function get_album_by_id($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM;
        $sql .= ' where id = ' . $id;
        $data = $db->fetchAll($sql);
        return $data[0];
    }
    
    public function update_girl_album_viewer($id, $view, $time) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Update " . self::$TABLE_NAME_GIRL_ALBUM;
        $sql .= " SET ";
        $sql .= "`create_time`" . "='" . $time . "',";
        $sql .= "`view`" . "='" . $view . "'";
        $sql .= ' where id = ' . $id;

        return $db->query($sql);
    }

    public function get_album_for_update_by_album_id($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM;
        $sql .= ' where `id` = ' . $id;
        $temp = $db->fetchAll($sql);
        $data['album'] = $temp[0];
        $temp = $this->get_album_detail_by_albumid($id);
        $imagelist = '';
        $length = count($temp);
        for ($i = 0; $i < $length; $i++) {
            $imagelist.=$temp[$i]['src'];
            if ($i != $length - 1)
                $imagelist.= "\n";
        }

        $data['album_detail'] = $imagelist;
        $data['album_detail_array'] = $temp;
        return $data;
    }

    public function get_album_detail_by_albumid($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM_DETAIL;
        $sql .= ' where album_id = ' . $id;
        $data = $db->fetchAll($sql);
        return $data;
    }
    
    public function update_girl_detail_album($id, $data) {
        if (!empty($data)) {
            $db = Model_Connections::getInstance()->getDbConnect();            
            $numRows = $db->insert(self::$TABLE_NAME_GIRL_ALBUM_DETAIL, $data);
            return $numRows;
        }
    }

    public function insert_girl_detail_album($data) {
        if (!empty($data)) {
            $db = Model_Connections::getInstance()->getDbConnect();
            $numRows = $db->insert(self::$TABLE_NAME_GIRL_ALBUM_DETAIL, $data);
            return $numRows;
        }
    }

    public function delete_girl_detail_albumid($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Delete FROM " . self::$TABLE_NAME_GIRL_ALBUM_DETAIL;
        $sql .= ' where album_id = ' . $id;
        return $db->query($sql);
    }

    public function get_album_total() {
        $db = Model_Connections::getInstance()->getDbConnect();
        $count = $db->fetchOne('SELECT COUNT(*) AS count FROM ' . self::$TABLE_NAME_GIRL_ALBUM);
        return $count;
    }

    public function get_album_by_page_toarray($page, $count) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM;
        $start_from = ($page - 1) * $count;
        $end = $count;
        $sql .= ' order by id DESC ';
        $sql .= ' LIMIT ' . $start_from . ',' . $end;
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function get_album_by_page($page, $count) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Select * from " . self::$TABLE_NAME_GIRL_ALBUM;
        $start_from = ($page - 1) * $count;
        $end = $count;
        $sql .= ' order by id DESC ';
        $sql .= ' LIMIT ' . $start_from . ',' . $end;
        $data = $db->fetchAll($sql);
        if(!empty($data)){
            foreach ($data as $k => $value) {
                unset($data[$k]['img']);
                unset($data[$k]['comment']);
                unset($data[$k]['admin']);
                unset($data[$k]['create_time']);
            }
        }
        return $data;
    }

    public function insert_girl_album($data) {
        if (!empty($data)) {
            $db = Model_Connections::getInstance()->getDbConnect();
            $id = $db->insert(self::$TABLE_NAME_GIRL_ALBUM, $data);
            $lastInsertId = -1;
            if ($id > 0)
                $lastInsertId = $db->lastInsertId();
            return $lastInsertId;
        }
    }

    public function delete_girl_album($id) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Delete from " . self::$TABLE_NAME_GIRL_ALBUM_DETAIL;
        $sql .= ' where album_id = ' . $id;
        $db->query($sql);
        $sql = "Delete from " . self::$TABLE_NAME_GIRL_ALBUM;
        $sql .= ' where id = ' . $id;
        return $db->query($sql);
    }

    public function update_girl_album($id, $data) {
        $db = Model_Connections::getInstance()->getDbConnect();
        $sql = "Update " . self::$TABLE_NAME_GIRL_ALBUM;
        $sql .= " SET ";
        $sql .= "`title`" . "='" . $data['title'] . "',";
        $sql .= "`src`" . "='" . $data['src'] . "',";
        $sql .= "`more`" . "='" . $data['more'] . "',";
        $sql .= "`admin`" . "='" . $data['admin'] . "'";
        $sql .= ' where id = ' . $id;

        return $db->query($sql);
    }

}
?>